﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using zs.Notifier.PersistenceStore;

namespace zs.Notifier
{
    public partial class Notifier : System.Web.UI.UserControl
    {
        protected override void CreateChildControls()
        {
            SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
            Notification notification = webApplication.GetChild<Notification>("Notify");

            if (notification.Display)
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine("<script>");

                //First wait until the SP.js is loaded, otherwise the Notification doesn't work and we get an null reference exception
                stringBuilder.AppendLine("ExecuteOrDelayUntilScriptLoaded(Initialize, \"sp.js\");");

                stringBuilder.AppendLine("function Initialize() {");
                stringBuilder.AppendLine("SP.UI.Notify.addNotification(\"" + notification.Message + "\", " + notification.Sticky.ToString().ToLower() + ", \"Tip\", null);");
                stringBuilder.AppendLine("}");

                stringBuilder.AppendLine("</script>");

                this.Controls.Add(new LiteralControl(stringBuilder.ToString()));
            }
            base.CreateChildControls();
        }

        
    }
}